package com.example.onlinemarket.controller;

import com.example.onlinemarket.bean.*;
import com.example.onlinemarket.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    private UserService userService;
    @Autowired
    public void setUserService(UserService userService){
        this.userService = userService;
    }

    public String home(User user){
        if(user!=null)
            return user.isRole()?"forward:/shop/shopIndex":"forward:/shopping/shoppingIndex";
        else return "index";
    }

    private final String path = System.getProperty("user.dir")+"\\src\\main\\resources\\static\\img\\tempUserImg.jpg";

    //登录
    @RequestMapping("/login")
    public String login(HttpServletRequest request, HttpSession session, Model model, User user){
        Logger logger = LoggerFactory.getLogger(LoginController.class);
        //若用户名/邮箱或密码为空，跳转回登录页面
        if((user.getUserName()==null||user.getUserName().equals(""))
        ||(user.getPassword()==null||user.getPassword().equals(""))){
            request.setAttribute("loginUser", user);
            request.setAttribute("msg", "用户名(邮箱)或密码错误！");
            return "index";
        }else {
            User loginUser = userService.canUserLogin(user.getUserName(), user.getUserName(), user.getPassword());
            //若返回的loginUser对象不为空则表示登录成功
            if (loginUser != null) {
                session = request.getSession();
                session.setAttribute("isLogin", true);
                session.setAttribute("userId", loginUser.getUserId());
                session.setAttribute("loginUser", loginUser);
                model.addAttribute("loginUser", loginUser);
                System.out.println("\n===登录成功===\n");
                logger.debug("\n===login-ok===\n");
                //根据用户身份跳转到对应界面
                return home(loginUser);
            } else {//登录失败，跳回登录界面
                request.setAttribute("loginUser", user);
                request.setAttribute("loginMsg", "用户名(邮箱)或密码错误！");
                return "index";
            }
        }
    }

    @RequestMapping("/isRegistered")
    public String isRegistered(Model model, User user){
        if(user.getUserName()!=null && user.getPassword()!=null
                &&!(user.getUserName().equals("") || user.getPassword().equals(""))) {
            //根据User对象注册新用户，传递回字符串信息msg表示是否注册成功
            String msg = userService.canUserRegister(user);
            if(!msg.equals("注册成功"))
                model.addAttribute("msg", msg);
        }else {
            model.addAttribute("registerUser", user);
            if(user.getUserName()==null||user.getUserName().equals("")) {
                String userNameMsg = "用户名不能为空！";
                model.addAttribute("userNameMsg", userNameMsg);
            }
            if(user.getPassword()==null||user.getPassword().equals("")){
                String passwordMsg = "密码不能为空！";
                model.addAttribute("passwordMsg", passwordMsg);
            }
        }
        return "index";
    }

    @RequestMapping("/logOut")
    public String logOut(HttpSession session){
        session.setAttribute("isLogin", false);
        session.setAttribute("userId", null);
        session.setAttribute("loginUser", null);
        return "index";
    }

}
